package com.example.React.singlethread;

import com.example.Constant;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * @author zhanghui
 * @date 2021/12/6 15:28
 */
public class EchoClient {
    public static void main(String[] args) throws IOException {
        new EchoClient().start();
    }

    private void start() throws IOException {
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(Constant.SERVER_IP, Constant.SERVER_PORT));
        socketChannel.configureBlocking(false);
        while (!socketChannel.finishConnect()) {

        }
        System.out.println("客户端启动成功");
        new Thread(new Processor(socketChannel)).start();
    }

    class Processor implements Runnable {
        private final Selector selector;
        private final SocketChannel socketChannel;

        public Processor(SocketChannel socketChannel) throws IOException {
            selector = Selector.open();
            this.socketChannel = socketChannel;
            socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
        }

        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    selector.select();
                    Set<SelectionKey> selectionKeys = selector.selectedKeys();
                    Iterator<SelectionKey> iterator = selectionKeys.iterator();
                    while (iterator.hasNext()) {
                        SelectionKey selectionKey = iterator.next();
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        if (selectionKey.isWritable()) {
                            Scanner scanner = new Scanner(System.in);
                            System.out.print("请输入发送内容: ");
                            if (scanner.hasNext()) {
                                String msg = scanner.nextLine();
                                byteBuffer.put(msg.getBytes());
                                byteBuffer.flip();
                                channel.write(byteBuffer);
                                byteBuffer.clear();
                            }
                        }
                        if (selectionKey.isReadable()) {
                            int length = 0;
                            while ((length = channel.read(byteBuffer)) > 0) {
                                byteBuffer.flip();
                                System.out.println("server echo: " + new String(byteBuffer.array(), 0, length));
                                byteBuffer.clear();
                            }
                        }
                    }
                    selectionKeys.clear();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
